﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
  public  class Update
    {
        /// <summary>
        /// 更新模型
        /// </summary>
        public static int UpdateModel(Models.Model model)
        {
            string sql = string.Format("update tb_model set Name=@Name,AdminDir=@AdminDir,WebDir=@WebDir,TableName=@TableName,Remark=@Remark where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID", SqlDbType.Int),
                                            new SqlParameter("Name",SqlDbType.NVarChar,50),
                                            new SqlParameter("AdminDir",SqlDbType.NVarChar,50),
                                            new SqlParameter("WebDir",SqlDbType.NVarChar,50),
                                            new SqlParameter("TableName",SqlDbType.NVarChar,50),
                                            new SqlParameter("Remark",SqlDbType.NVarChar,500)
                                        };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.AdminDir;
            parameters[3].Value = model.WebDir;
            parameters[4].Value = model.TableName;
            parameters[5].Value = model.Remark;

            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }
      /// <summary>
      /// 更新用户
      /// </summary>
      /// <param name="user"></param>
      /// <param name="Simple"></param>
      /// <returns></returns>
        public static int UpdateUser(Models.User user)
        {
            string sql = string.Format("update tb_user set Name=@Name,Password=@Password,RoleID=@RoleID,UserEnabled=@UserEnabled where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID", SqlDbType.Int),
                                            new SqlParameter("Name", SqlDbType.NVarChar,50),
                                            new SqlParameter("Password", SqlDbType.NVarChar, 255),
                                            new SqlParameter("RoleID", SqlDbType.Int),
                                            new SqlParameter("UserEnabled", SqlDbType.Bit)
                                        };
            parameters[0].Value = user.ID;
            parameters[1].Value = user.Name;
            parameters[2].Value = user.Password;
            parameters[3].Value = user.RoleID;
            parameters[4].Value = user.UserEnabled;

            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        public static int UpdateRole(Models.Role role)
        {
            string sql = string.Format("update tb_role set Name=@Name,RoleInfoModelID=@RoleInfoModelID,IsAdmin=@IsAdmin,ModelAdmin=@ModelAdmin,ColumnAdmin=@ColumnAdmin,ContentAdmin=@ContentAdmin,UserAdmin=@UserAdmin,RoleAdmin=@RoleAdmin,FileAdmin=@FileAdmin,LogAdmin=@LogAdmin,SystemAdmin=@SystemAdmin,RoleEnabled=@RoleEnabled where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID", SqlDbType.Int),
                                            new SqlParameter("Name", SqlDbType.NVarChar, 50),
                                            new SqlParameter("RoleInfoModelID", SqlDbType.Int),
                                            new SqlParameter("IsAdmin", SqlDbType.Bit),
                                            new SqlParameter("ModelAdmin", SqlDbType.Bit),
                                            new SqlParameter("ColumnAdmin", SqlDbType.Bit),
                                            new SqlParameter("ContentAdmin", SqlDbType.NVarChar, int.MaxValue),
                                            new SqlParameter("UserAdmin", SqlDbType.Bit),
                                            new SqlParameter("RoleAdmin", SqlDbType.Bit),
                                            new SqlParameter("FileAdmin", SqlDbType.Bit),
                                            new SqlParameter("LogAdmin", SqlDbType.Bit),
                                            new SqlParameter("SystemAdmin", SqlDbType.Bit),
                                            new SqlParameter("RoleEnabled", SqlDbType.Bit)
                                        };
            parameters[0].Value = role.ID;
            parameters[1].Value = role.Name;
            parameters[2].Value = role.RoleInfoModelID;
            parameters[3].Value = role.IsAdmin;
            parameters[4].Value = role.ModelAdmin;
            parameters[5].Value = role.ColumnAdmin;
            parameters[6].Value = role.ContentAdmin;
            parameters[7].Value = role.UserAdmin;
            parameters[8].Value = role.RoleAdmin;
            parameters[9].Value = role.FileAdmin;
            parameters[10].Value = role.LogAdmin;
            parameters[11].Value = role.SystemAdmin;
            parameters[12].Value = role.RoleEnabled;

            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }
        /// <summary>
        /// 更新指定栏目
        /// </summary>
        public static int UpdateColumn(Models.Column column)
        {
            string sql = string.Format("update tb_column set Name=@Name,ParentID=@ParentID,ModelID=@ModelID,ColumnLogo=@ColumnLogo,Remark=@Remark where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID",SqlDbType.Int),
                                            new SqlParameter("Name", SqlDbType.NVarChar, 50),
                                            new SqlParameter("ParentID", SqlDbType.Int),
                                            new SqlParameter("ModelID", SqlDbType.Int),
                                             new SqlParameter("ColumnLogo", SqlDbType.NVarChar, 50),
                                            new SqlParameter("Remark", SqlDbType.NVarChar, 500)
                                        };
            parameters[0].Value = column.ID;
            parameters[1].Value = column.Name;
            parameters[2].Value = column.ParentID;
            parameters[3].Value = column.ModelID;
            parameters[4].Value = column.ColumnLogo;
            parameters[5].Value = column.Remark;

            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }
        /// <summary>
        /// 更新栏目排序信息
        /// </summary>
        public static int UpdateSortColumn(Models.Column column)
        {
            string sql = string.Format("update tb_column set ParentID=@ParentID,OrderID=@OrderID  where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID",SqlDbType.Int),                                   
                                            new SqlParameter("ParentID", SqlDbType.Int),
                                            new SqlParameter("OrderID", SqlDbType.Int),                                                                   
                                        };
            parameters[0].Value = column.ID;
            parameters[1].Value = column.ParentID;
            parameters[2].Value = column.OrderID;


            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }
        /// <summary>
        /// 更新文章模型内容
        /// </summary>
        public static int UpdateArticle(Models.ModelArticle article)
        {
            string sql = string.Format("update tb_content_article set LastEditor=@LastEditor,ColumnID=@ColumnID,Title=@Title,Content=@Content,UpdateDate=@UpdateDate,Source=@Source,Status=@Status where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID", SqlDbType.Int),
                                            new SqlParameter("LastEditor", SqlDbType.NVarChar, 50),                                            
                                            new SqlParameter("ColumnID", SqlDbType.Int),
                                            new SqlParameter("Title", SqlDbType.NVarChar, 50),
                                            new SqlParameter("Content", SqlDbType.NVarChar,int.MaxValue),
                                            new SqlParameter("UpdateDate", SqlDbType.DateTime),
                                            new SqlParameter("Source", SqlDbType.NVarChar, 100),
                                            new SqlParameter("Status", SqlDbType.Bit)
                                        };
            parameters[0].Value = article.ID;
            parameters[1].Value = article.LastEditor;
            parameters[2].Value = article.ColumnID;
            parameters[3].Value = article.Title;
            parameters[4].Value = article.Content;
            parameters[5].Value = article.UpdateDate;
            parameters[6].Value = article.Source;
            parameters[7].Value = article.Status;

            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }
        /// <summary>
        /// 更新单页模型内容
        /// </summary>
        public static int UpdateContent(Models.ModelContent content)
        {
            string sql = string.Format("update tb_content set LastEditor=@LastEditor,ColumnID=@ColumnID,Title=@Title,Content=@Content,UpdateDate=@UpdateDate where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID", SqlDbType.Int),
                                            new SqlParameter("LastEditor", SqlDbType.NVarChar, 50),                                            
                                            new SqlParameter("ColumnID", SqlDbType.Int),
                                            new SqlParameter("Title", SqlDbType.NVarChar, 50),
                                            new SqlParameter("Content", SqlDbType.NText),
                                            new SqlParameter("UpdateDate", SqlDbType.DateTime)
                                        };
            parameters[0].Value = content.ID;
            parameters[1].Value = content.LastEditor;
            parameters[2].Value = content.ColumnID;
            parameters[3].Value = content.Title;
            parameters[4].Value = content.Content;
            parameters[5].Value = content.UpdateDate;

            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }
        /// <summary>
        /// 更新图片轮播模型内容
        /// </summary>
        public static int UpdateSlider(Models.ModelSlider slider)
        {
            string sql = string.Format("update tb_content_slider set LastEditor=@LastEditor,ColumnID=@ColumnID,Title=@Title,UpdateDate=@UpdateDate,PictureAddress=@PictureAddress,ArticleAddress=@ArticleAddress,Remark=@Remark where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID", SqlDbType.Int),
                                            new SqlParameter("LastEditor", SqlDbType.NVarChar, 50),                                            
                                            new SqlParameter("ColumnID", SqlDbType.Int),
                                            new SqlParameter("Title", SqlDbType.NVarChar, 50),                                    
                                            new SqlParameter("UpdateDate", SqlDbType.DateTime),                                     
                                            new SqlParameter("PictureAddress", SqlDbType.NVarChar, 100),
                                            new SqlParameter("ArticleAddress", SqlDbType.NVarChar, 100),
                                            new SqlParameter("Remark", SqlDbType.NVarChar, 500)
                                        };
            parameters[0].Value = slider.ID;
            parameters[1].Value = slider.LastEditor;
            parameters[2].Value = slider.ColumnID;
            parameters[3].Value = slider.Title;
            parameters[4].Value = slider.UpdateDate;
            parameters[5].Value = slider.PictureAddress;
            parameters[6].Value = slider.ArticleAddress;
            parameters[7].Value = slider.Remark;

            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }
        /// <summary>
        /// 更新外链模型内容
        /// </summary>
        public static int UpdateLink(Models.ModelLink link)
        {
            string sql = string.Format("update tb_content_link set LastEditor=@LastEditor,ColumnID=@ColumnID,WebUrl=@WebUrl,UpdateDate=@UpdateDate,WebName=@WebName,WebLogo=@WebLogo where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID", SqlDbType.Int),
                                            new SqlParameter("LastEditor", SqlDbType.NVarChar, 50),                                            
                                            new SqlParameter("ColumnID", SqlDbType.Int),
                                            new SqlParameter("WebUrl", SqlDbType.NVarChar, 50),                                    
                                            new SqlParameter("UpdateDate", SqlDbType.DateTime),                                     
                                            new SqlParameter("WebName", SqlDbType.NVarChar, 50),
                                            new SqlParameter("WebLogo", SqlDbType.NVarChar, 50),
                                         
                                        };
            parameters[0].Value = link.ID;
            parameters[1].Value = link.LastEditor;
            parameters[2].Value = link.ColumnID;
            parameters[3].Value = link.WebUrl;
            parameters[4].Value = link.UpdateDate;
            parameters[5].Value = link.WebName;
            parameters[6].Value = link.WebLogo;     

            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }
        /// <summary>
        /// 更新办公文件模型内容
        /// </summary>
        public static int UpdateOfficeFile(Models.ModelOfficeFile file)
        {
            string sql = string.Format("update tb_content_officefile set FileName=@FileName,FileEditor=@FileEditor,UploadTime=@UploadTime,UploadSource=@UploadSource where ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("ID", SqlDbType.Int),                                                                   
                                            new SqlParameter("FileName", SqlDbType.NVarChar,50),
                                            new SqlParameter("FileEditor", SqlDbType.NVarChar, 50),                                    
                                            new SqlParameter("UploadTime", SqlDbType.DateTime),                                     
                                            new SqlParameter("UploadSource", SqlDbType.NVarChar, 50),                                     
                                         
                                        };
            parameters[0].Value = file.ID;
            parameters[1].Value = file.FileName;
            parameters[2].Value = file.FileEditor;
            parameters[3].Value = file.UploadTime;
            parameters[4].Value = file.UploadSource;

            Utility.SQLHelper db = new Utility.SQLHelper();
            return db.ExecuteNonQuery(sql, parameters, CommandType.Text);
        }
    }
}
